package com.sino.struts2;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.googlecode.jsonplugin.annotations.SMDMethod;
import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.IBasicInfoService;
import com.sino.business.IStaticDataService;
import com.sino.business.service.LoginInfo;
import com.sino.model.BasicInfoModel;
import com.sino.second.bean.TblSystemparam;
import com.sino.second.model.SystemparamModel;

public class BasicInfoAction extends ActionSupport {

	private IStaticDataService staticDateService;// 基础数据

	private IBasicInfoService basicInfoService;// 基本信息

	private BasicInfoModel basicinfoModel;// 基本信息基础类�����

	private SystemparamModel systemparamModel;

	private SystemparamModel systemparamModel2;

	private List hyList;

	private List xkList;

	private List zlTypeList;

	private List zlType1List;

	private List xkzlList;

	private List areaList;

	private List flztList;

	private List qxkwList;

	private String zlid;

	private String selectLianDong;

	// -------显示基本信息页面��ʾ����
	public String viewBasicInfo() throws Exception {
		this.setHyList(this.getStaticDateService().findHyList());
		this.setXkList(this.getStaticDateService().findXkList());
		this.setZlTypeList(this.getStaticDateService().findZlTypeList());
		this.setZlType1List(this.getStaticDateService().findZlType1List());
		this.setXkzlList(this.getStaticDateService().findXkzlList());
		this.setAreaList(this.getStaticDateService().findAreaList());
		this.setFlztList(this.getStaticDateService().findFlztList());
		this.setQxkwList(this.getStaticDateService().findQxkwList());
		this.setSelectLianDong(this.getStaticDateService()
				.findZlTypeListSelect());

		String time = this.basicInfoService.findShuJuKuTime();
		if ("1".equals(time)) {
			if (this.getBasicInfoService().validateExistZlId(
					this.getBasicinfoModel().getZlid2())) {
				// 如果专利号码存在跳转到修改页面
				String zlid = this.getBasicInfoService().validateLogin(
						basicinfoModel.getZlid2(), basicinfoModel.getPassw());
				if (zlid == null) {
					throw new Exception("专利号码或密码错误，请重新输入！");
				} else if (zlid.equals("1")) {
					throw new Exception("此专利申请已经被审核，不允许修改，如果想要修改请与推荐机构联系！");
				} else {
					HttpSession session = ServletActionContext.getRequest()
							.getSession();
					session.setAttribute("zlid2", zlid);
				}
				return this.INPUT;
			} else {
				return this.SUCCESS;
			}
		} else {
			throw new Exception("不是申报日期，请确认时间后进行申报");
		}
	}

	// xyw 时间设置

	public String displayTime() {
		systemparamModel = new SystemparamModel();
		systemparamModel2 = new SystemparamModel();
		systemparamModel.setParamName("beginTime");
		systemparamModel2.setParamName("endTime");
		systemparamModel.setParamValue(this.basicInfoService.displayTime());
		systemparamModel2.setParamValue(this.basicInfoService.displayTime2());
		return this.SUCCESS;
	}

	// -----------显示修改密码页面
	public String viewModifyPassw() throws Exception {
		String zlid = this.getBasicInfoService().validateLogin(
				basicinfoModel.getZlid2(), basicinfoModel.getPassw());
		if (zlid == null) {
			throw new Exception("该专利号为第一次登录，不能修改密码，请以任意密码先登录！");
		}
		return this.SUCCESS;
	}

	// ------------修改密码
	public String updatePassw() {
		this.getBasicInfoService().updatePassw(
				this.getBasicinfoModel().getZlid2(),
				this.getBasicinfoModel().getPassw());
		return this.SUCCESS;
	}

	// ---------保存基本信息
	public String saveBasicInfo() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Calendar calendar = Calendar.getInstance();// 获得Calendar实例
		basicinfoModel.setYear(new Integer(calendar.get(Calendar.YEAR))
				.toString());
		this.getBasicInfoService().saveBasicInfo(basicinfoModel);
		session.setAttribute("zlid", basicinfoModel.getZlid());
		return this.SUCCESS;
	}

	// ---------更新基本信息
	public String updateBasicInfo() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Calendar calendar = Calendar.getInstance();// 获得Calendar实例
		basicinfoModel.setYear(new Integer(calendar.get(Calendar.YEAR))
				.toString());
		this.getBasicInfoService().saveBasicInfo(basicinfoModel);
		session.setAttribute("zlid2", basicinfoModel.getZlid());
		return this.SUCCESS;
	}

	// --------根据zlid查找基本信息
	public String findBasicInfo() {

		this.setHyList(this.getStaticDateService().findHyList());
		this.setXkList(this.getStaticDateService().findXkList());
		this.setZlTypeList(this.getStaticDateService().findZlTypeList());
		this.setZlType1List(this.getStaticDateService().findZlType1List());
		this.setXkzlList(this.getStaticDateService().findXkzlList());
		this.setAreaList(this.getStaticDateService().findAreaList());
		this.setFlztList(this.getStaticDateService().findFlztList());
		this.setQxkwList(this.getStaticDateService().findQxkwList());
		this.setSelectLianDong(this.getStaticDateService()
				.findZlTypeListSelect());

		HttpSession session = ServletActionContext.getRequest().getSession();
		String zlId = (String) session.getAttribute("zlid");
		this.setBasicinfoModel(this.getBasicInfoService().findBasicinfoById(
				zlId));
		return this.SUCCESS;
	}

	public String findBasicInfoToPrint() {
		this.setHyList(this.getStaticDateService().findHyList());
		this.setXkList(this.getStaticDateService().findXkList());
		this.setZlTypeList(this.getStaticDateService().findZlTypeList());
		this.setZlType1List(this.getStaticDateService().findZlType1List());
		this.setXkzlList(this.getStaticDateService().findXkzlList());
		this.setAreaList(this.getStaticDateService().findAreaList());
		this.setFlztList(this.getStaticDateService().findFlztList());
		this.setQxkwList(this.getStaticDateService().findQxkwList());
		this.setSelectLianDong(this.getStaticDateService()
				.findZlTypeListSelect());

		this.setBasicinfoModel(this.getBasicInfoService().findBasicinfoById(
				zlid));

		return this.SUCCESS;
	}

	// --------根据zlid查找基本信息---用于更新
	public String findUpdateBasicInfo() {
		this.setHyList(this.getStaticDateService().findHyList());
		this.setXkList(this.getStaticDateService().findXkList());
		this.setZlTypeList(this.getStaticDateService().findZlTypeList());
		this.setZlType1List(this.getStaticDateService().findZlType1List());
		this.setXkzlList(this.getStaticDateService().findXkzlList());
		this.setAreaList(this.getStaticDateService().findAreaList());
		this.setFlztList(this.getStaticDateService().findFlztList());
		this.setQxkwList(this.getStaticDateService().findQxkwList());
		this.setSelectLianDong(this.getStaticDateService()
				.findZlTypeListSelect());
		HttpSession session = ServletActionContext.getRequest().getSession();
		String zlId = (String) session.getAttribute("zlid2");
		this.setBasicinfoModel(this.getBasicInfoService().findBasicinfoById(
				zlId));
		return this.SUCCESS;
	}

	public String delBasicInfo() {
		this.getBasicInfoService().delBasicInfo(basicinfoModel, zlid);

		return this.SUCCESS;
	}

	// 申请人登陆
	public String login() throws Exception {
		String zlid = this.getBasicInfoService().validateLogin(
				basicinfoModel.getZlid(), basicinfoModel.getPassw());
		if (zlid == null) {
			throw new Exception("专利号码或密码错误，请重新输入！");
		} else if (zlid.equals("1")) {
			throw new Exception("此专利申请已经被审核，不允许修改，如果想要修改请与推荐机构联系！");
		} else {
			HttpSession session = ServletActionContext.getRequest()
					.getSession();
			session.setAttribute("zlid2", zlid);
		}
		return this.SUCCESS;
	}

	// 退出
	public String logout() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("zlid");
		return this.SUCCESS;
	}

	// 登陆退出退出
	public String logoutLogin() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("zlid2");
		return this.SUCCESS;
	}

	// -----------验证专利号是否重复==实际专利号码
	@SMDMethod
	public LoginInfo doSomething(LoginInfo loginInfo) {
		boolean retn = this.getBasicInfoService().validateExistZlId(
				loginInfo.getName());
		if (retn) {
			loginInfo.setName("exist");
		} else {
			loginInfo.setName("no");
		}
		return loginInfo;
	}

	public String validateExist() {
		return this.SUCCESS;
	}

	// --------------------------------------------------------接口
	public IBasicInfoService getBasicInfoService() {
		return basicInfoService;
	}

	public void setBasicInfoService(IBasicInfoService basicInfoService) {
		this.basicInfoService = basicInfoService;
	}

	public BasicInfoModel getBasicinfoModel() {
		return basicinfoModel;
	}

	public void setBasicinfoModel(BasicInfoModel basicinfoModel) {
		this.basicinfoModel = basicinfoModel;
	}

	public IStaticDataService getStaticDateService() {
		return staticDateService;
	}

	public void setStaticDateService(IStaticDataService staticDateService) {
		this.staticDateService = staticDateService;
	}

	public List getAreaList() {
		return areaList;
	}

	public void setAreaList(List areaList) {
		this.areaList = areaList;
	}

	public List getFlztList() {
		return flztList;
	}

	public void setFlztList(List flztList) {
		this.flztList = flztList;
	}

	public List getHyList() {
		return hyList;
	}

	public void setHyList(List hyList) {
		this.hyList = hyList;
	}

	public List getQxkwList() {
		return qxkwList;
	}

	public void setQxkwList(List qxkwList) {
		this.qxkwList = qxkwList;
	}

	public List getXkList() {
		return xkList;
	}

	public void setXkList(List xkList) {
		this.xkList = xkList;
	}

	public List getXkzlList() {
		return xkzlList;
	}

	public void setXkzlList(List xkzlList) {
		this.xkzlList = xkzlList;
	}

	public List getZlTypeList() {
		return zlTypeList;
	}

	public void setZlTypeList(List zlTypeList) {
		this.zlTypeList = zlTypeList;
	}

	public String getZlid() {
		return zlid;
	}

	public void setZlid(String zlid) {
		this.zlid = zlid;
	}

	public String getSelectLianDong() {
		return selectLianDong;
	}

	public void setSelectLianDong(String selectLianDong) {
		this.selectLianDong = selectLianDong;
	}

	public List getZlType1List() {
		return zlType1List;
	}

	public void setZlType1List(List zlType1List) {
		this.zlType1List = zlType1List;
	}

	public SystemparamModel getSystemparamModel() {
		return systemparamModel;
	}

	public void setSystemparamModel(SystemparamModel systemparamModel) {
		this.systemparamModel = systemparamModel;
	}

	public SystemparamModel getSystemparamModel2() {
		return systemparamModel2;
	}

	public void setSystemparamModel2(SystemparamModel systemparamModel2) {
		this.systemparamModel2 = systemparamModel2;
	}
}
